<!--
 * @Description: 选择转办人
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2022-10-14 18:22:06
 * @LastEditTime: 2022-12-01 17:21:51
 * @FilePath: \cscec-pms-admin\src\views\matters\approval\components\consultation\SelectTurnTo.vue
-->
<template>
  <a-modal
    title="选择转办人"
    :width="750"
    :visible="visible"
    :cancel-text="'关闭'"
    :ok-text="'提交'"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-table
      :row-key="'id'"
      :columns="columns"
      :data-source="data"
      :pagination="false"
      bordered
      class="select-turn-to-table requiredFormItem"
    >
      <template
        slot="tranfer"
        slot-scope="text, record"
      >
        <SelectStaffWithTooltip
          v-model="record.tranfer"
          label-in-value
          placeholder="请选择转办人"
          :not-myself="true"
          :request-params="
            {
              types:[0]
            }
          "
        />
      </template>
    </a-table>
    <custom-form
      ref="form"
      :is-validate="true"
      :edit-model="''"
      :rules="rules"
      :form="form"
      :form-list="formOption"
      :is-emit="false"
      class="requiredFormItem"
    />
  </a-modal>
</template>
<script>
import { formOption1, columns2 } from './model.js'
import { tranferTask } from '@/api/process'
import SelectStaffWithTooltip from '@/components/Select/SelectStaffWithTooltip'
import { notifyParentWindow } from '@/utils/util.js'
export default {
  name: 'SelectTurnTo',
  components: {
    SelectStaffWithTooltip
  },
  props: {
    // 模态框显示隐藏
    visible: {
      type: Boolean,
      default: false
    },
    // 当前节点
    taskName: {
      type: String,
      require: true,
      default: ''
    },
    // 登录人信息
    info: {
      type: Object,
      default: () => {}
    },
    //是否关闭页面
    isClose: {
      type: Boolean,
      default: true
    },
     complaintTaskId:{
      type:String,
      default:''
    }
  },
  data() {
    return {
      // 表头
      columns: columns2,
      // 表格数据
      data: [{ id: new Date().getTime() + '', taskName: this.taskName }],
      // 表单项
      formOption: formOption1,
      // 表单对象
      form: {},
      // 提交loading
      confirmLoading: false
    }
  },
  watch: {
    // 设置当前节点
    taskName(val) {
      this.data[0].taskName = val
    }
  },
  computed: {
    // 表单校验
    rules() {
      const obj = {}
      this.formOption.map((item) => {
        if (item.rules) {
          obj[item.keyName] = item.rules
        }
      })
      return obj
    }
  },
  methods: {
    /**
     * @description: 转办
     * @return {void}
     */
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.data[0]?.tranfer?.key) {
            this.$confirm({
              title: '警告提醒！',
              content: '确定审批转办？',
              okText: '确认',
              cancelText: '取消',
              onOk: () => {
                return new Promise(async (resolve) => {
                  try {
                    const { id: userId } = this.info
                    const res = await tranferTask({
                      opinions: this.form.opinions,
                      receiveId: this.data[0].tranfer.key,
                      taskId: !this.complaintTaskId?this.$route.query.taskId:this.complaintTaskId,
                      userId
                    })
                    if (res.data.code === this.SUCCESS_CODE) {
                      this.$message.success('转办成功')
                      this.handleCancel()
                      notifyParentWindow()
                      //成功移除批量审批数据
                      this.$emit('removeApproval')
                      this.isClose && window.close()
                    }
                    resolve()
                  } catch (error) {
                    console.log(error)
                    resolve()
                  }
                })
              }
            })
          } else {
            this.$message.warning('请选择转办人')
          }
        }
      })
    },
    /**
     * @description: 取消
     * @return {void}
     */
    handleCancel() {
      this.data[0].tranfer = ''
      this.form = {}
      this.$emit('update:visible', false)
    }
  }
}
</script>
<style lang="less" scoped>
.select-turn-to-table {
  margin-bottom: 20px;
}
</style>
